//
//  BindUserIdView.swift
//  loveHHEveryDay
//
//  Created by yw on 2025/4/15.
//

import SwiftUI
import Lottie



struct BindUserIdView: View {
    @EnvironmentObject var pathManager:PathManager
    @State var userId:String = ""
    @State var showAnim:Bool = false
    var body: some View {
        //        NavigationStack {
        VStack {
            ScrollView(showsIndicators: false) {
                userIdInput
                tipsTitle
                confirmBtnView
                if showAnim {
                    LottieView()
                        .frame(maxWidth: 300)
                        .frame(height: 400)
                        .background(.clear)
                        .offset(y:-200)
                }
            }
            
        }
        .onTapGesture {
            UIApplication.shared.endEditing()
        }
        .navigationTitle("用户绑定")
        //        .navigationBarTitleDisplayMode(.inline)
        .frame(maxWidth: .infinity)
        .background(Color("color_bg"))
        .navigationBack()
    }
    var tipsTitle:some View {
        HStack {
            Text("输入需要绑定的用户id,既可以关联用户")
                .modifier(YWTextModifer(fontSize: 14, fontWeight: .light, design: .rounded, color: .colorTextSub))
            Spacer()
        }
        .padding(.top,4)
        .padding(.horizontal,14)
    }
    
    var userIdInput:some View {
        HStack {
            TextField("请输入用户id", text: $userId)
            Image(systemName: "xmark.circle.fill")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 24, height: 24)
                .accentColor(Color("color_text_main"))
                .shakeTapGes {
                    clean()
                }
        }
        .padding(.horizontal,10)
        .frame(height: 50)
        .background(.white)
        .cornerRadius(14)
        .padding(.horizontal,10)
        .padding(.top,20)
    }
    
    var confirmBtnView:some View {
        ShakeButton(label: Text("开始绑定"), textColor: .colorTextMain, fontSize: 16, fontWeight: .bold) {
            confirmBtnClick()
        }.padding(.top,60)
    }
    
    private func clean() {
        userId = ""
    }
    
    private func confirmBtnClick() {
        if userId.isEmpty {
            YWToast.show(text: "用户id不能为空")
            return
        }
        if userId.count != 10 {
            YWToast.show(text: "输入格式有误，请重试")
            return
        }
        
        showAnim.toggle()
        let generator = UIImpactFeedbackGenerator(style: .heavy)
        generator.prepare()
        generator.impactOccurred()
        YWToast.show(text: "绑定成功")
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.3) {
            pathManager.path.removeLast()
        }
    }
}


struct LottieView: UIViewRepresentable {
    var name = "success"
//    var loopMode:Bool = false
//    var animationSpeed:CGFloat = 1
    
    func makeUIView(context: UIViewRepresentableContext<LottieView>) -> UIView {
        let view = UIView(frame: CGRectMake(0, 0, 300, 300))
        let lottieView = LottieAnimationView(name: "success")
        lottieView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(lottieView)
        NSLayoutConstraint.activate([
            lottieView.heightAnchor.constraint(equalTo: view.heightAnchor),
            lottieView.widthAnchor.constraint(equalTo: view.widthAnchor)
        ])
        lottieView.loopMode = .playOnce
        lottieView.animationSpeed = 1
        lottieView.play()
        return view
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) {
    }
}
